//
//  OIWorkInfoDetailViewController.m
//  EducationOID
//
//  Created by slz on 2017/6/6.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIWorkInfoDetailViewController.h"
#import "OIPubListViewController.h"
#import "OIWorkInfoModel.h"
#import "NSString+OIDAnalysis.h"

@interface OIWorkInfoDetailViewController ()<MFWDataObserver>
@property (nonatomic,strong) PubListTableHeadView *headerView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *contentLabel;
@end

@implementation OIWorkInfoDetailViewController
{
    OIWorkInfoModel *_model;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDetailInfoWithId:self.itemId];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"信息中心"];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;

    [self initWithHeaderView];
    // Do any additional setup after loading the view.
}

- (void)initWithHeaderView{
    _headerView = [[PubListTableHeadView alloc]init];
    _headerView.titleLabel.text = @"OID注册公示列表";
    _headerView.size = CGSizeMake(kScreenWidth, 80);
    
    _scrollView = [UIScrollView new];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    _contentView = [UIView new];
    [_scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.mas_equalTo(666);
    }];
    
    [_contentView addSubview:_headerView];
    
    _titleLabel = [UILabel new];
    _titleLabel.fontSize = 15;
    _titleLabel.textColor = kDark_TextColor_Default;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(90);
        make.centerX.mas_equalTo(_contentView.mas_centerX);
    }];
    
    _contentLabel = [UILabel new];
    _contentLabel.fontSize = 13;
    _contentLabel.numberOfLines = 0;
    _contentLabel.textColor = kLight_TextColor_Default;
    [_contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentView.mas_left).offset(11);
        make.right.mas_equalTo(_contentView.mas_right).offset(-11);
        make.top.mas_equalTo(120);
    }];
}

-(void)requestDetailInfoWithId:(NSString *)actionId {
    if(_model == nil) {
        _model = [OIWorkInfoModel managedModelInstance];
        [[_model class] addDataObserver:self];
    }
    [_model requestRefresh:[_model dataWithActionId:actionId]];
}

-(void)requestFinishedForTask:(MFWTask *)task withModel:(MFWDataModel *)model dataId:(NSString *)dataId requestType:(MFWDataRequestType)type itemArray:(NSArray *)itemArray success:(BOOL)bSuccess error:(NSError *)error{
    if (bSuccess && !error){
        MFWJson * json = [model jsonDataForDataId:dataId];
        _titleLabel.text = [json stringValueForKey:@"title"];
        NSString * replaced = [self getZZwithString:[json stringValueForKey:@"content"]];
        _contentLabel.text = replaced;
    }else{
        [self.view showTip:@"请求错误"];
    }
}

-(NSString *)getZZwithString:(NSString *)string{
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"[a-z&;/]"
                                                                                    options:0
                                                                                      error:nil];
    
    string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    return string;
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
